package com.elite.groovy.callback

import com.elite.groovy.kh.KHDBConfig
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.kh.LogToDB
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @projectName: Groovy*
 * @package: com.elite.groovy.callback*
 * @className: apiWoMessageEntrance*
 * @description: 用于工单操作时，添加工单消息推送任务。之后由定时任务统一发送
 * @author: tian*
 * @date: 2023-11-03 14:30
 * @version: 1.0
 */

class apiWoMessage implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiWoMessage.class)

    String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis()
        response.setCharacterEncoding("UTF-8")
        response.setContentType("application/json;charset=utf-8")

        // 初始化返回参数
        JSONObject resultJson = new JSONObject()
        JSONArray resonArray = new JSONArray()

        String getMethod = request.getMethod()
        String inData = ""
        if (getMethod.equals("GET")) {
            inData = request.getParameter("data")
            log.debug("[原始Get请求参数]:" + inData)
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8")
            BufferedReader iny = new BufferedReader(reader)
            String line
            while ((line = iny.readLine()) != null) {
                inData += line
            }
            log.debug("[原始Post请求参数]:" + inData)
        }
        log.info("[调用请求信息]:" + inData)


        //入参为空
        if (KHUtil.isNull(inData)) {
            resultJson.put("resultcode", -2)
            resultJson.put("resultmessage", "待发送内容为空!")
            log.debug("【apiWoMessage】入参为空-无法完成操作!")
            return resultJson.toString()
        }

        //业务内容
        int count = 0
        int cgcount = 0
        int sbcount = 0
        try {
            JSONObject inJson = JSONObject.fromObject(inData)
            if (KHUtil.GetSFJV(inJson, "op_type") == "防伪异常反馈结果") {
                resultJson = notifyCust(eliteBean, inJson, "防伪异常反馈结果");
            } else if (KHUtil.GetSFJV(inJson, "op_type") == "投诉满意度短信发送") {
                resultJson = ComplaintSatisfactionnotifyCust(eliteBean, inJson, "投诉满意度短信发送");
            } else {
                JSONArray body = inJson.getJSONArray("body")
                //入参为空
                if (body.size() <= 0) {
                    resultJson.put("resultcode", -2)
                    resultJson.put("resultmessage", "待发送任务为空!")
                    log.debug("【apiWoMessage】body数组为空-无法完成操作!")
                    return resultJson.toString()
                }
                count = body.size();//获取需要处理的数据总数

                String send_type = KHUtil.GetSFJV(inJson, "send_type") //操作类型
                String operate_staff = KHUtil.GetSFJV(inJson, "operate_staff") //操作类型
                String comments = KHUtil.GetSFJV(inJson, "comments") //备注

                String getQwidSQL = "select staffname from staff where staff_id='" + operate_staff + "'";//企业微信id
                List QwList = eliteBean.chaxun_sql(getQwidSQL, "dataSource");
                if (QwList.size() > 0) {
                    operate_staff = QwList.get(0).get("staffname");
                }

                //操作类型为空
                if (KHUtil.isNull(send_type)) {
                    resultJson.put("resultcode", -2)
                    resultJson.put("resultmessage", "操作类型为空!")
                    log.debug("【apiWoMessage】操作类型为空-无法完成操作!")
                    return resultJson.toString()
                }
                //根据当前工单操作获取短信模板配置。
                String queryDoctemplateSql = "select sw.workorderstepstatus_id,sw.workorderstepstatusdesc,d.doctemplate_id,d.description,d.documentpath,sw.dynid,if(d.documentpath2 is null or d.documentpath2 = '',d.documentpath,d.documentpath2) as documentpath2 from sysc_workorderstepstatus sw left join doctemplate d on sw.doctemplate=d.doctemplate_id where workorderstepstatus_id='" + send_type + "'";
                log.info("执行任务数据查询SQL:" + queryDoctemplateSql)
                List doctemplateList = eliteBean.chaxun_sql(queryDoctemplateSql, "dataSource")
                String doctemplate_id = "";
                String description = "";
                String documentpath = "";
                String dynid = "";//操作对应的动态页面id
                String documentpath2 = "";//操作对应的动态页面id
                String comments2 = "";//催单 处理意见 标识

                if (doctemplateList.size() > 0) {
                    doctemplate_id = doctemplateList.get(0).get("doctemplate_id");//工单操作类型配置的通知模板
                    description = doctemplateList.get(0).get("description");//工单操作类型配置的通知模板描述，作为通知的标题使用
                    documentpath = doctemplateList.get(0).get("documentpath");//工单操作类型配置的通知模板内容
                    dynid = doctemplateList.get(0).get("dynid");//工单操作对应的动态页面id
                    documentpath2 = doctemplateList.get(0).get("documentpath2");//企业微信通知需要的json报文
                } else {
                    resultJson.put("resultcode", -2)
                    resultJson.put("resultmessage", "操作类型不正确!")
                    log.debug("【apiWoMessage】操作类型不正确-无法完成操作!")
                    return resultJson.toString()
                }

                for (int t = 0; t < body.size(); t++) {
                    JSONObject resonObj = new JSONObject()
                    String objective_guid = "";
                    String elitetask_guid = "";
                    try {
                        JSONObject bodyjson = body.getJSONObject(t);

                        objective_guid = KHUtil.GetSFJV(bodyjson, "objective_guid") //工单编号
                        List recipient_person = body.getJSONObject(t).containsKey("recipient_person") ? body.getJSONObject(t).getJSONArray("recipient_person") : new ArrayList(); //本条工单需要通知的人

                        //工单编号必传
                        if (KHUtil.isNull(objective_guid)) {
                            String checkNullInfo = KHUtil.isNullMSG("objective_guid", objective_guid)
                            resonObj.put(objective_guid, checkNullInfo)
                            resonArray.push(resonObj)
                            sbcount = sbcount + 1
                            break
                        }
                        String queryObjectiveSql = '''Select o.Objective_Guid,
                                                           o.Customer_Guid,
                                                           o.Workorderid,
                                                           Date_Format(o.Createddate, '%Y年%m月%d日') As Createddate,
                                                           Wf.Mapname,
                                                           e.Elitetask_Guid,
                                                           Sf.Userid
                                                      From Objective o
                                                      Left Join Elitetask e
                                                        On o.Objective_Guid = e.Objective_Guid
                                                      Left Join Wo3_Frame_Map Wf
                                                        On o.Map_Id = Wf.Map_Id
                                                      Left Join Staff Sf
                                                        On o.Handleby_Id = Sf.Staff_Id
                                                     Where 1=1
                                                       And o.Objective_Guid = \'''' + objective_guid + '''\'''';

                        //工单主键ID
                        List objectiveList = eliteBean.chaxun_sql(queryObjectiveSql, "dataSource")
                        log.info("执行任务数据查询SQL结果:" + objectiveList)
                        String customer_guid = "";
                        String workorderid = "";
                        String createddate = "";
                        String mapname = "";
                        JSONObject contentJSON = new JSONObject()
                        if (objectiveList.size() > 0) {
                            customer_guid = objectiveList.get(0).get("Customer_Guid");
                            workorderid = objectiveList.get(0).get("Workorderid");
                            createddate = objectiveList.get(0).get("Createddate");
                            mapname = objectiveList.get(0).get("Mapname");
                            elitetask_guid = objectiveList.get(0).get("Elitetask_Guid");

                            // 如果通知人为空则直接取工单当前处理人
                            if (recipient_person.size() == 0) {
                                String user_Id = objectiveList.get(0).get("Userid");
                                if (!KHUtil.isNull(user_Id)) {

                                    recipient_person.add(objectiveList.get(0).get("Userid"));
                                    log.warn("如果通知人为空则直接取工单当前处理人:" + recipient_person);
                                } else {
                                    resonObj.put(objective_guid, "当前催办工单未找到处理人或处理人企微绑定异常,请联系管理员")
                                    resonArray.push(resonObj)
                                    sbcount = sbcount + 1;
                                    continue;
                                }
                            }

                            //模板内容
                            contentJSON.put("staffname", operate_staff);
                            contentJSON.put("workorderid", workorderid);
                            contentJSON.put("createddate", createddate);
                            contentJSON.put("mapname", mapname);
                            if (send_type == "URGE") {
                                contentJSON.put("comments", comments);
                            }

                        }
                        log.debug("工单内容：" + contentJSON.toString())

                        //开始拼接短信内容
                        String re_dynid = ""
                        String re_message_content = ""
                        String re_message_path = ""
                        JSONObject reDoctemplate = getDoctemplate(doctemplate_id, '8', contentJSON, documentpath, dynid, documentpath2)
                        log.debug(reDoctemplate.toString())

                        if (reDoctemplate.get("code") == "1") {
                            String re_method = reDoctemplate.getJSONObject("data").get("send_method")
                            re_message_content = reDoctemplate.getJSONObject("data").get("to_message_content")
                            re_message_path = reDoctemplate.getJSONObject("data").get("to_message_path")
                            re_dynid = reDoctemplate.getJSONObject("data").get("dynid");
                            log.debug("催单企微通知：" + re_message_content)

                            //企业微信需校验报文内容
                            //企业微信需校验报文内容
                            if (KHUtil.isNull(re_message_path)) {
                                resonObj.put(objective_guid, "未获取到企微模板或拼接错误" + doctemplate_id)
                                resonArray.push(resonObj)
                                sbcount = sbcount + 1
                                break
                            }

                        } else {
                            //其他原因失败的
                            resonObj.put(objective_guid, reDoctemplate.get("message"))
                            resonArray.push(resonObj)
                            log.debug(reDoctemplate.get("message"))
                            sbcount = sbcount + 1//记录失败条数
                            break
                        }

                        //添加通知任务
                        int executeRow = 0;
                        for (int j = 0; j < recipient_person.size(); j++) {
                            String userid = recipient_person.get(j);
                            // 如果模板里面有配置对应的动态页面ID-则开始获取对于的手机端界面获取NGS菜单短码
                            String linkURL = ""
                            if (!KHUtil.isNull(re_dynid)) {
                                String DOMAIN = KHDBConfig.getAPIURL(eliteBean, "SYS-OuterDomain")
                                DOMAIN = DOMAIN.endsWith("/") ? DOMAIN : DOMAIN + "/"

                                //采用企业微信打开自动获取用户信息授权的方式
                                linkURL = DOMAIN + "groovy/GeneralService/apiNGSLogin?opdynid=" + re_dynid + "&opsource=wxq&taskid=" + objective_guid + "&redirect=1";
                            }

                            executeRow = sendDataDB(eliteBean, '8', re_message_content, re_message_path, send_type, customer_guid, objective_guid, "0", "", userid, doctemplate_id, linkURL)
                            log.debug(userid + "执行结果：" + executeRow)

                        }
                        if (executeRow == 1) {
                            cgcount = cgcount + 1
                        } else {
                            resonObj.put(objective_guid, "INSERT新增短信任务失败")
                            resonArray.push(resonObj)
                            sbcount = sbcount + 1
                        }

                    } catch (Exception e) {
                        resonObj.put(objective_guid, "服务内部错误")
                        resonArray.push(resonObj)
                        log.debug(e.getMessage())
                        sbcount = sbcount + 1
                    }
                }

                long endTime = System.currentTimeMillis()
                resultJson.put("resultcode", "1")
                resultJson.put("resultmessage", "接口调度成功,总共调度条数：" + count + "条,成功" + cgcount + "条,失败：" + sbcount + "条,总共调度耗时(毫秒)：" + (endTime - startTime))
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e)
            resultJson.put("resultcode", "0")
            resultJson.put("resultmessage", e.getMessage())
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "添加消息通知任务[内部接口]", "apiWoMessage", inData, resultJson.toString(), "本次发送总耗时：" + (System.currentTimeMillis() - startTime).toString(), getMethod)
        }

        resultJson.put("total", count)
        resultJson.put("succcount", cgcount)
        resultJson.put("failount", sbcount)
        resultJson.put("output", resonArray)
        return resultJson.toString()
    }

    //获取短信模板&拼接替换短信内容
    static JSONObject getDoctemplate(String doctemplate_id, String send_method, JSONObject reqJson, String documentpath, String dynid, String documentpath2) {
        JSONObject Json = new JSONObject()
        try {
            String to_message_content = documentpath
            String to_message_documentpath2 = documentpath2;

            Iterator<String> keys = reqJson.keys();

            while (keys.hasNext()) {
                String key = keys.next();
                to_message_content = to_message_content.replace("#" + key + "#", reqJson.getString(key));
                to_message_documentpath2 = to_message_documentpath2.replace("#" + key + "#", reqJson.getString(key));

            }

            //执行成功返回结果
            JSONObject jsonContent = new JSONObject()
            jsonContent.put("to_message_content", to_message_content)
            jsonContent.put("to_message_path", to_message_documentpath2)
            jsonContent.put("dynid", dynid)
            jsonContent.put("send_method", send_method)

            Json.put("code", "1")
            Json.put("message", "内容拼接执行成功" + doctemplate_id)
            Json.put("data", jsonContent)

        } catch (Exception e) {
            log.debug(e.getMessage())
            Json.put("code", "0");
            Json.put("message", e.getMessage());
            Json.put("data", {});
        }
        return Json
    }

    // 工单回复通知客户
    public static JSONObject notifyCust(EliteBean eliteBean, JSONObject inJson, String op_Type) {
        log.info("工单回复通知客户 信息op_Type：" + op_Type + " inJson:" + inJson);
        JSONObject res_Json = JSONObject.fromObject("{\"resultcode\":\"0\",\"resultmessage\":\"\"}");

        if (op_Type == "防伪异常反馈结果") {

            // 获取关键参数
            String obje_Guid = KHUtil.GetSFJV(inJson, "objective_guid");

            String Doctemplate_Id = KHDBConfig.getParamConfig(eliteBean, "防伪异常反馈结果_To_Cust");
            Doctemplate_Id = KHUtil.isNull(Doctemplate_Id) ? "5KJ7JR" : Doctemplate_Id;

            // 查询工单关键信息
            String select_SQL = '''Select o.Workorderid,
                                           w.Mapname,
                                           o.Customer_Guid,
                                           e.Elitetask_Guid,
                                           Date_Format(o.Createddate, '%Y-%m-%d') As Createddate,
                                           o.Modifieddate,
                                           t.Customername,
                                           t.Mobile,
                                           d.Documentpath,
                                           d.Dynid
                                      From Objective o
                                      Left Join Elitetask e
                                        On o.Objective_Guid = e.Objective_Guid
                                      Left Join Tabcworkorder t
                                        On o.Objective_Guid = t.Relation_Id
                                      Left Join Wo3_Frame_Map w
                                        On o.Map_Id = w.Map_Id
                                      Left Join Doctemplate d
                                        On d.Doctemplate_Id = \'''' + Doctemplate_Id + '''\'
                                     Where o.Map_Id = '1EMZZA' And o.Objective_Guid = \'''' + obje_Guid + '''\' Limit 1''';
            List<Map<String, Object>> select_Res = eliteBean.chaxun_sql(select_SQL, "dataSource");
            log.warn("查询工单对于的数据信息select_Res:" + select_Res);

            if (select_Res.size() > 0) {
                Map<String, Object> select_Map = select_Res.get(0);
                String Workorderid = GetMapV(select_Map, "Workorderid");
                String Mapname = GetMapV(select_Map, "Mapname");
                String Customer_Guid = GetMapV(select_Map, "Customer_Guid");
                String Elitetask_Guid = GetMapV(select_Map, "Elitetask_Guid");
                String Createddate = GetMapV(select_Map, "Createddate");
                String Modifieddate = GetMapV(select_Map, "Modifieddate");
                String Customername = GetMapV(select_Map, "Customername");
                String Mobile = GetMapV(select_Map, "Mobile");
                String Documentpath = GetMapV(select_Map, "Documentpath");
                String Dynid = GetMapV(select_Map, "Dynid");

                // 生成短连接
                String linkURL = ""
                if (!KHUtil.isNull(Dynid)) {
                    String DOMAIN = KHDBConfig.getAPIURL(eliteBean, "SYS-InnerDomain");
                    DOMAIN = DOMAIN.endsWith("/") ? DOMAIN : DOMAIN + "/";
                    String apiLinkJson = Util.doHttpGet(DOMAIN + "groovy/GeneralService/apiShortenLink?dynid=" + Dynid + "&userid=" + Customer_Guid + "&usertype=cust&woid=" + obje_Guid + "&taskid=" + Elitetask_Guid + "&service=FW_FWYC&expand=", "UTF-8", 5000, 0, "");
                    JSONObject reLinkJson = JSONObject.fromObject(apiLinkJson);
                    log.debug("获取的短链接地址返回报文:" + reLinkJson.toString());

                    if (reLinkJson.getInt("code") != 1) {
                        linkURL = "";
                    } else {
                        linkURL = KHUtil.GetSFJV(reLinkJson, "message")

                    }
                }
                if (select_Res.size() > 0) {
                    // 尊敬的消费者您好，您于#date#提交的#workorderno#异常反馈已处理完毕，查看结果请点击#url# 。
                    linkURL = Documentpath.contains("https://ccc.davco.cn/") ? linkURL.replace("https://ccc.davco.cn/", "") : (Documentpath.contains("http://ccc.davco.cn/") ? linkURL.replace("http://ccc.davco.cn/", "") : linkURL);

                    Documentpath = Documentpath.replace("#date#", Createddate).replace("#workorderno#", Workorderid).replace("#url#", linkURL);

                    int executeRow = sendDataDB(eliteBean, "3", Documentpath, Documentpath, "防伪异常反馈结果", Customer_Guid, obje_Guid, "", "", Mobile, Doctemplate_Id, linkURL);
                    res_Json.put("resultcode", "1");
                    res_Json.put("resultmessage", "短链接生成成功,发送客户完成executeRow:" + executeRow);

                } else {
                    log.warn("短链接生成异常,停止发送客户");
                    res_Json.put("resultcode", "-1");
                    res_Json.put("resultmessage", "短链接生成异常,停止发送客户linkURL:" + linkURL);

                }

            } else {
                log.warn("未查询工单对于的数据信息");
                res_Json.put("resultcode", "-1");
                res_Json.put("resultmessage", "未查询工单对于的数据信息obje_Guid:" + obje_Guid);

            }


        } else {
            log.warn("暂不支持的操作类型");
            res_Json.put("resultcode", "-1");
            res_Json.put("resultmessage", "暂不支持的操作类型op_Type:" + op_Type);
        }

        log.info("工单回复通知客户 返回结果：" + res_Json);
        return res_Json;
    }

    // 投诉满意度通知客户
    public static JSONObject ComplaintSatisfactionnotifyCust(EliteBean eliteBean, JSONObject inJson, String op_Type) {
        log.info("投诉满意度短信发送 信息op_Type：" + op_Type + " inJson:" + inJson);
        JSONObject res_Json = JSONObject.fromObject("{\"resultcode\":\"0\",\"resultmessage\":\"\"}");

        if (op_Type == "投诉满意度短信发送") {

            String Doctemplate_Id = KHDBConfig.getParamConfig(eliteBean, "投诉满意度短信发送_To_Cust");
            Doctemplate_Id = KHUtil.isNull(Doctemplate_Id) ? "E3BNSK" : Doctemplate_Id;

            // 获取关键参数
            String obje_Guid = "";
            String objective_guids = KHUtil.GetSFJV(inJson, "objective_guid");
            String[] obje_arr = objective_guids.split(","); // 根据逗号进行切割
            if (obje_arr.length > 0) {
                // 循环取数
                for (int i = 0; i < obje_arr.length; i++) {
                    obje_Guid = obje_arr[i];
                    // 查询工单关键信息
                    String select_SQL = '''Select o.Workorderid,
												   w.Mapname,
												   o.Customer_Guid,
												   e.Elitetask_Guid,
												   o.Createddate,
												   o.Modifieddate,
												   t.Customername,
												   t.Mobile,
												   d.Documentpath,
												   d.Dynid
											  From Objective o
											  Left Join Elitetask e
												On o.Objective_Guid = e.Objective_Guid
											  Left Join Tabcworkorder t
												On o.Objective_Guid = t.Relation_Id
											  Left Join Wo3_Frame_Map w
												On o.Map_Id = w.Map_Id
											  Left Join Doctemplate d
												On d.Doctemplate_Id = \'''' + Doctemplate_Id + '''\'
											 Where o.Objective_Guid = \'''' + obje_Guid + '''\' Limit 1''';
                    List<Map<String, Object>> select_Res = eliteBean.chaxun_sql(select_SQL, "dataSource");
                    log.warn("查询工单对于的数据信息select_Res:" + select_Res);

                    if (select_Res.size() > 0) {
                        Map<String, Object> select_Map = select_Res.get(0);
                        String Workorderid = GetMapV(select_Map, "Workorderid");
                        String Mapname = GetMapV(select_Map, "Mapname");
                        String Customer_Guid = GetMapV(select_Map, "Customer_Guid");
                        String Elitetask_Guid = GetMapV(select_Map, "Elitetask_Guid");
                        String Createddate = GetMapV(select_Map, "Createddate");
                        String Modifieddate = GetMapV(select_Map, "Modifieddate");
                        String Customername = GetMapV(select_Map, "Customername");
                        String Mobile = GetMapV(select_Map, "Mobile");
                        String Documentpath = GetMapV(select_Map, "Documentpath");
                        String Dynid = GetMapV(select_Map, "Dynid");

                        // 生成短连接
                        String linkURL = ""
                        if (!KHUtil.isNull(Dynid)) {
                            String DOMAIN = KHDBConfig.getAPIURL(eliteBean, "SYS-InnerDomain");
                            DOMAIN = DOMAIN.endsWith("/") ? DOMAIN : DOMAIN + "/";
                            String apiLinkJson = Util.doHttpGet(DOMAIN + "groovy/GeneralService/apiShortenLink?dynid=" + Dynid + "&userid=" + Customer_Guid + "&usertype=cust&woid=" + obje_Guid + "&taskid=" + Elitetask_Guid + "&service=FW_FWYC&expand=", "UTF-8", 5000, 0, "");
                            JSONObject reLinkJson = JSONObject.fromObject(apiLinkJson);
                            log.debug("获取的短链接地址返回报文:" + reLinkJson.toString());

                            if (reLinkJson.getInt("code") != 1) {
                                linkURL = "";
                            } else {
                                linkURL = KHUtil.GetSFJV(reLinkJson, "message")

                            }
                        }
                        if (select_Res.size() > 0) {
                            // 尊敬的客户，您好！您于#date#反馈的问题现已处理完毕，您的感受对我们非常重要，诚邀您对本次售后服务进行评价( #url# )，感谢您的支持！如有任何疑问请致电4008009889，我们将竭诚为您服务。
                            linkURL = Documentpath.contains("https://ccc.davco.cn/") ? linkURL.replace("https://ccc.davco.cn/", "") : (Documentpath.contains("http://ccc.davco.cn/") ? linkURL.replace("http://ccc.davco.cn/", "") : linkURL);

                            Documentpath = Documentpath.replace("#date#", Createddate).replace("#url#", linkURL);

                            int executeRow = sendDataDB(eliteBean, "3", Documentpath, Documentpath, "投诉满意度短信发送", Customer_Guid, obje_Guid, "", "", Mobile, Doctemplate_Id, linkURL);
                            res_Json.put("resultcode", "1");
                            res_Json.put("resultmessage", "短链接生成成功,发送客户完成executeRow:" + executeRow);

                        } else {
                            log.warn("短链接生成异常,停止发送客户");
                            res_Json.put("resultcode", "-1");
                            res_Json.put("resultmessage", "短链接生成异常,停止发送客户linkURL:" + linkURL);

                        }

                    } else {
                        log.warn("未查询工单对于的数据信息");
                        res_Json.put("resultcode", "-1");
                        res_Json.put("resultmessage", "未查询工单对于的数据信息obje_Guid:" + obje_Guid);

                    }
                }

            } else {
                log.warn("传入的obje_arr为空");
                res_Json.put("resultcode", "-1");
                res_Json.put("resultmessage", "传入的obje_arr为空:" + obje_Guid);
            }

        } else {
            log.warn("暂不支持的操作类型");
            res_Json.put("resultcode", "-1");
            res_Json.put("resultmessage", "暂不支持的操作类型op_Type:" + op_Type);
        }

        log.info("投诉满意度短信发送 返回结果：" + res_Json);
        return res_Json;
    }

    // 解析Mapn值(此方法增加需要解析的key不存在保护)
    public static String GetMapV(Map inMap, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = String.valueOf(inMap.get(getKey));
        } catch (Exception e) {

            Rev_Value = "";
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            }
        }
        return Rev_Value;
    }

    // 添加document表，发送数据DB处理
    static int sendDataDB(EliteBean eliteBean, String Doctype, String Documentpath, String Documentpath2, String Comments, String Customer_Guid, String Objective_Guid, String documentfrom, String Stringfield1, String documentto, String Template_Id, String linkURL) {
        long startTime = System.currentTimeMillis()
        log.debug("开始将发送内容数据处理到DB:" + startTime)
        int executeRow = 0
        try {
            def InsertSQL = "Insert Into Documentout" +
                    "  (Documentout_Guid," +
                    "   Doctype," +
                    "   Status," +
                    "   Trialcount," +
                    "   Trialcountlimit," +
                    "   Documentpath," +
                    "   Comments," +
                    "   Documentto," +
                    "   Priority_Id," +
                    "   Customer_Guid," +
                    "   Objective_Guid," +
                    "   Createdby," +
                    "   Createddate," +
                    "   documentfrom," +
                    "   Stringfield1," +
                    "   Documentpath2," +
                    "   Template_Id," +
                    "   Expectedstartdate," +
                    "   send_url)" +
                    "Values" +
                    "  (Uuid()," +
                    "   '" + Doctype + "'," +
                    "   0," +
                    "   0," +
                    "   5," +
                    "   '" + Documentpath + "'," +
                    "   '" + Comments + "'," +
                    "   '" + documentto + "'," +
                    "   '1'," +
                    "   '" + Customer_Guid + "'," +
                    "   '" + Objective_Guid + "'," +
                    "   'SELITE'," +
                    "   Now()," +
                    "   '" + documentfrom + "'," +
                    "   '" + Stringfield1 + "'," +
                    "   '" + Documentpath2 + "'," +
                    "   '" + Template_Id + "'," +
                    "   Now()," +
                    "   '" + linkURL + "')"
            executeRow = eliteBean.update(InsertSQL, "dataSource")

            long endTime = System.currentTimeMillis()
            log.debug("发送内容数据处理到DB时长(毫秒):" + (endTime - startTime))

        } catch (Exception e) {
            long endTime = System.currentTimeMillis()
            log.error("发送内容数据处理到DB时长(毫秒):" + (endTime - startTime) + " 处理失败(" + e.getMessage() + ")")
        }

        return executeRow
    }
}